<!DOCTYPE html>
<html>
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no"/>
    <title>添加或编辑界面</title>
    <link rel="stylesheet" type="text/css" href="${s.base}/lib/layui/css/layui.css"/>
    <link rel="stylesheet" type="text/css" href="${s.base}/css/base/index.css"/>
    <link rel="stylesheet" type="text/css" href="${s.base}/css/base/theme/theme.css"/>
    <style>
        .w300{
            width: 300px !important;
        }
        .marr50{margin-right:50px!important;}
        .layui-inline{ margin-right:0!important;}
    </style>
</head>
<body>
<div class="page-content-wrap clearfix">
    <form class="layui-form" id="">
        <div class="layui-tab">
            <div class="layui-tab-content">
                <div class="div-tit-top"><span></span>招生信息</div>
                <div class="layui-tab-item layui-show">
                    <div class="layui-form-item">
                        <label class="layui-form-label"><span class="red">*</span>标题：</label>
                        <div class="layui-input-inline w300 marr50">
                            <input type="text" name="name" id="titleName" value="${wxAssociationNew.title}" lay-verify="required" placeholder="请输入标题" autocomplete="off" class="layui-input" maxlength="200">
                        </div>
                        <label class="layui-form-label"><span class="red">*</span>最多报社团数：</label>
                        <div class="layui-input-inline w300">
                            <input type="text" name="name" id="maxNumber" value="${wxAssociationNew.maxAssoNum}" lay-verify="required" placeholder="请输入最多报社团数" autocomplete="off" class="layui-input" maxlength="200">
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label"><span class="red">*</span>开始日期：</label>
                            <div class="layui-input-inline w300 marr50">
                                <input type="text" name="startDate" id="startDate" value="<#if wxAssociationNew.beginDate??>${wxAssociationNew.beginDate?string('yyyy-MM-dd')}</#if>"  lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                            </div>
                            <label class="layui-form-label"><span class="red">*</span>结束日期：</label>
                            <div class="layui-input-inline w300">
                                <input type="text" name="endDate" id="endDate" value="<#if wxAssociationNew.endDate??>${wxAssociationNew.endDate?string('yyyy-MM-dd')}</#if>" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label"><span class="red">*</span>开始时间：</label>
                        <div class="layui-input-inline w300 marr50">
                            <input type="text" name="startTime" id="startTime" lay-verify="date" value="${wxAssociationNew.beginTime}" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                        </div>
                        <label class="layui-form-label"><span class="red">*</span>结束时间：</label>
                        <div class="layui-input-inline w300">
                            <input type="text" name="endTime" id="endTime" lay-verify="date" value="${wxAssociationNew.endTime}" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
                        </div>
                    </div>
                </div>
                </div>

                <div class="div-tit-top"><span></span>社团招新对象</div>
                <form class="layui-form">
                    <div class="search-cont clearfix" id="tool">
                        <div class="operation-btn-cont">
                            <div class="operright cyt-retract-close hideMenu"> 收起筛选<i class="iconfont">&#xe658;</i></div>
                            <div class="operright cyt-retract-open showMenu" style="display: none;"> 展开筛选<i class="iconfont">&#xe936;</i></div>
                            <button class="layui-btn layui-btn-normal layui-btn-mid" type="button" id="selectFile"><i class="iconfont marr5">&#xe612;</i>人员导入</button>
                            <button class="layui-btn layui-btn-normal layui-btn-mid" type="button" id="exportExcel"><i class="iconfont marr5">&#xe612;</i>模板下载</button>
                            <div class="triangle-bottom"></div>
                            <div class="triangle-top"></div>
                        </div>
                        <div class="search-details">
                            <div class="layui-form-item marb5">
                                <!--search开始-->
                                <div class="layui-inline">
                                    <label class="layui-form-label" >姓名：</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="userName" name="userName" placeholder="请输入姓名"  autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label" >学号：</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="userNumber" name="title" placeholder="请输入学号"  autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <div class="layui-inline">
                                    <label class="layui-form-label" >年级：</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="gradeName" name="title" placeholder="请输入年级" autocomplete="off" class="layui-input">
                                    </div>
                                </div>

                                <div class="layui-inline">
                                    <label class="layui-form-label" >班级：</label>
                                    <div class="layui-input-inline">
                                        <input type="text" id="className" name="title" placeholder="请输入班级" autocomplete="off" class="layui-input">
                                    </div>
                                </div>
                                <!--search结束-->
                                <div class="layui-inline marl8">
                                    <button class="layui-btn layui-btn-normal" type="button" id="searchBtn">搜索</button>
                                    <button class="layui-btn layui-btn-normal" type="button" id="resetBtn">重置</button>
                                </div>
                            </div>
                        </div>
                        <table class="table-list-tr" id="showTable">
                            <thead>
                                <tr>
                                    <th>序号</th>
                                    <th>学号</th>
                                    <th>姓名</th>
                                    <th>年级</th>
                                    <th>班级</th>
                                    <th>操作</th>
                                </tr>
                            </thead>
                            <tbody>
                            </tbody>
                        </table>
                        <div id="page">
                        </div>
                        <div class="div-tit-top" style="margin-top:20px;"><span></span>招新社团</div>
                        <div>
                            <button class="layui-btn layui-btn-normal layui-btn-mid" type="button" id="addassociation">添加社团</button>
                            <button class="layui-btn layui-btn-normal layui-btn-mid" type="button" id="addassociationSet">设置招新人数</button>
                        </div>
                        <div class="society-list clearfix" id="content">
                        </div>
                        <div id="pageother">
                        </div>
                    </div>
                </form>
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block" style="margin-left: 0px !important;text-align: center">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="" id="public">发布</button>
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="" id="comment">保存</button>
                <button type="button" id="cancel" class="layui-btn layui-btn-primary">取消</button>
                <button type="button" style="display: none;" id="save" class="layui-btn layui-btn-primary"></button>
            </div>
        </div>
    </form>
</div>
<script src="${s.base}/lib/layui/layui.js" type="text/javascript" charset="utf-8"></script>
<script src="${s.base}/mobile/js/common.js" type="text/javascript" charset="utf-8"></script>
<script src="${s.base}/js/common.js" type="text/javascript" charset="utf-8"></script>
<script>

    var applyId='${applyId}';
    var menuId='${parentMenuId}';
    var page = null;
    var baseutil = null;
    var dialog = null;
    var form = null;
    var newAssocaitionId ='${id}';
    var associationNumber=0;
    var associationPeopleNumber='1';
    var associationStu='1';
    var associationSelectIds ='';
    var curr = 1;// 当前页，初始值设为 1
    var limit = 10;// 每页条数，初始值设为 10
    var total;// 总记录数
    layui.use(['form', 'jquery','table', 'fileUtil', 'dialog', 'layer','page', 'layedit', 'baseutil','laydate','upload'], function () {
        var $ = layui.jquery,

            baseutil = layui.baseutil,
            laydate = layui.laydate,
            upload = layui.upload,
            page = layui.page,
            dialog = layui.dialog;
            table = layui.table,

        laydate.render({
            elem: '#startDate', //指定元素
            type: 'date'
        });
        laydate.render({
            elem: '#endDate', //指定元素
            type: 'date'
        });
        laydate.render({
            elem: '#startTime', //指定元素
            type: 'time'
        });
        laydate.render({
            elem: '#endTime', //指定元素
            type: 'time'
        });

        $(document).ready(function () {
            //一进入就硬删除那写明没有用的数据
            baseutil.get('/work/wxassociationnewstu/wxassociationnewstu/delete', {"associationNewId":"88888888"}, function (result) {
                if (0 == result.code) {
                    getInfo();// 获取数据
                    toPage();// 进行分页

                }
            });

            if(newAssocaitionId.length>0){
                getassociationMessage(newAssocaitionId);
                toPageOther();
            }
        });

        // 查询
        $('#searchBtn').bind('click', function () {
            getInfo();// 获取数据
            toPage();// 进行分页



        });
        // 重置
        $('#resetBtn').bind('click', function () {
            $("#userNumber").val("");
            $("#gradeName").val("");
            $("#className").val("");
            $("#userName").val("");
            getInfo();// 获取数据
            toPage();// 进行分页

        });

        function getInfo() {
            var userNumber = $("#userNumber").val();
            var gradeName = $("#gradeName").val();
            var className = $("#className").val();
            var userName = $("#userName").val();
            if(newAssocaitionId.length>0){
            }else {
                //刚刚导入的人员显示在上面其他的人员不显示在上面
                newAssocaitionId ="88888888";
            }
            $.ajax({
                type: "post",
                url: "/work/wxassociationnewstu/wxassociationnewstu/findPage",
                async: false,// 设置同步请求，加载数据前锁定浏览器
                dataType: 'json',
                data: {
                    "page": curr, // 查询页码
                    "limit": limit, // 每页条数
                    "userNumber":userNumber,
                    "userName":userName,
                    "gradeName":gradeName,
                    "className":className,
                    "applyId":applyId,
                    "associationNewId":newAssocaitionId

                },
                success: successFu
            });
        }
        $('#content').on('click','.delect-association', function () {
            page.delete('/work/wxassocationnewDetail/wxassociationnewdetail/remove', {"id": $(this).attr("data-id")}, "您确定要删除吗？", function () {
                getassociationMessage(newAssocaitionId);
                toPageOther();
            });
        });

        function getassociationMessage(newAssocaitionId) {
            $.ajax({
                type: "post",
                url: "/work/wxassocationnewDetail/wxassociationnewdetail/findVoPage",
                async: false,// 设置同步请求，加载数据前锁定浏览器
                dataType: 'json',
                data: {
                    "page": curr, // 查询页码
                    "limit": limit, // 每页条数
                    "applyId":applyId,
                    "associationNewId":newAssocaitionId
                },
                success: successMessage
            });
        }

        function successMessage(pager) {
            $("#content").html("");
            associationNumber = pager.count;
            total = pager.count;

            if (pager.total == 0) {
                return;
            }
            associationSelectIds='';
            var selectArray= [];
            var text = '<ul>';
            $.each(pager.data, function (i, item) {
                debugger;
                var  pathString = '/sysFile/showFileByPath?path=' + item.path;
                //只要有一个为空的就会影响
                associationPeopleNumber ='1';
                if(item.totalNumber=="0"){
                    associationPeopleNumber ='0';
                }
                selectArray.push(item.associationId);
                text+='<li>'+
                    '<div class="top-btn" style="display:none;">'+
                    '<div class="top-btn-list">'+
                    '<button type="button" class="menu-bg-red delect-association" data-id="'+item.id+'">删除</button>'+
                    '</div>'+
                    '</div>'+
                    '<div class="toppic" style="background: url('+pathString+') top center no-repeat;background-size: cover;"></div>'+
                    '</div>'+
                    '<h1>'+item.title+'</h1>'+
                    '<h2>指导老师：<span>'+item.userName+'</span></h2>'+
                    '<h2>招新<span class="num-blue">'+item.totalNumber+'</span>人，已招<span class="num-blue">'+item.stuNumber+'</span> 人</h2>'+
                    '</li>';
            });
            text +='</ul>';
            $("#content").html(text);
            if(selectArray.length>0){
                associationSelectIds = selectArray.join(",");
            }
            $(function(){
                $(".society-list ul li").mouseover(function(){+
                    $(".society-list ul li").find(".top-btn").hide();
                    $(this).find(".top-btn").show();
                })
                $(".society-list ul li").mouseout(function(){
                    $(".society-list ul li").find(".top-btn").hide();
                    //$(this).find(".top-btn").hide();
                })
            })


        }

        function successFu(pager) {
            associationStu = pager.count;
            total = pager.count;
            // 3.渲染数据
            // 当前查询无数据时
            if (pager.total == 0) {
                return;
            }
            var text = '';
            $.each(pager.data, function (i, item) {
                var mark =  (i * 1 + ((curr * 1 - 1) * limit + 1) * 1);
                text +='<tr>'+

                '<td>'+mark+'</td>'+
                '<td>'+item.userNumber+'</td>'+
                 '<td>'+item.userName+'</td>'+
                    '<td>'+item.gradeName+'</td>'+
                    '<td>'+item.className+'</td>'+
                    '<td><span style="cursor:pointer" class="iconfont" data-id="'+item.id+'">&#xe6a3;</span></td>'+
                        '</tr>';
            });
            $("#showTable tbody").html(text);
            $("#showTable tbody tr").on('click','.iconfont',function () {
                page.delete('/work/wxassociationnewstu/wxassociationnewstu/delete', {"id": $(this).attr("data-id")}, "您确定要删除吗？", function () {
                    getInfo();
                    toPage();
                });

            })
        }
        upload.render({
            elem: '#selectFile'
            ,url: '/work/wxassociationnewstu/wxassociationnewstu/importExcel'
            , accept: 'file'
            , data: {associationNewId: newAssocaitionId,applyId:applyId}
            , multiple: true
            , auto: true
            ,before:function(obj){
                layer.load();
            }
            ,done:function(res){
                curr=1;
                layer.closeAll('loading');
                if (res.code === 0) {
                    dialog.msg(res.msg);
                    console.log(res.data);
                    getInfo();
                    toPage();

                } else {
                    dialog.msg(res.msg);
                }
            }
        });

        function validNewBaseMsg() {
            var title = $("#titleName").val();
            var beginDateStr = $("#startDate").val();
            var endDateStr = $("#endDate").val();
            var beginTime = $("#startTime").val();
            var endTime = $("#endTime").val();
            var maxAssoNum = $("#maxNumber").val();
            if (isBlank(title) || isBlank(beginDateStr) || isBlank(endDateStr) || isBlank(beginTime) || isBlank(endTime)
                || isBlank(maxAssoNum)) {

                dialog.msg("请完善招新信息再添加社团！");
                return false;
            }
            var nowTime = FormatMyTime(new Date(), "yyyy-MM-dd");
            var result = checkEndTime(nowTime, beginDateStr);
            if (!result) {
                dialog.msg("开始日期不能小于当前日期！");
                return false;
            }
            result = checkEndTime(beginDateStr, endDateStr);
            if (!result) {
                dialog.msg("开始日期不能大于结束日期！");
                return false;
            }
            result = checkEndTime(nowTime + " " + beginTime, nowTime + " " + endTime);
            if (!result) {
                dialog.msg("开始时间段不能大于结束时间段！");
                return false;
            }
            if(associationStu=="0"||associationStu.length==0){
                dialog.msg("请导入学生！");
                return false;
            }

            return true;
        }

        function addAssociationPeople(number) {
            var data ={number:number,
                id:newAssocaitionId
            };
            baseutil.post('/work/associationNew/wxassociationnew/associationNumberSet',data, function (res) {
                dialog.msg("设置成功");
                getassociationMessage(newAssocaitionId);
                toPageOther();
            });
        }
        function addAssocationByDialg(associationIds) {
           //保存信息
            var Str ="";
            if(newAssocaitionId =="88888888"){
            }else {
                Str = newAssocaitionId;
            }

            var title = $("#titleName").val().trim();
            var beginDateStr = $("#startDate").val();
            var endDateStr = $("#endDate").val();
            var beginTime = $("#startTime").val();
            var endTime = $("#endTime").val();
            var maxAssoNum = $("#maxNumber").val();
            var data ={title:title,
                applyId:applyId,
                beginDate:beginDateStr,
                endDate:endDateStr,
                beginTime:beginTime,
                endTime:endTime,
                maxAssoNum:maxAssoNum,
                status:"3",
                associationIds:associationIds,
                id:Str
            };
            baseutil.post('/work/associationNew/wxassociationnew/addOrUpdate',data, function (res) {
                dialog.msg("保存成功");
                if(newAssocaitionId.length==0){
                    newAssocaitionId = res.data;
                    getassociationMessage(newAssocaitionId);
                }else if(newAssocaitionId=="88888888"){
                    newAssocaitionId = res.data;
                    getassociationMessage(newAssocaitionId);
                }

                else {
                    getassociationMessage(newAssocaitionId);
                }

                toPageOther();
            });
        }

        function updateAssciation(status) {
            if (validNewBaseMsg()) {
                if(associationNumber.length==0||associationNumber==0){
                    dialog.msg("请添加社团！");
                    return false;
                }
                if(associationPeopleNumber=="0"){
                    dialog.msg("请设置招新人数！");
                    return false;
                }

                var title = $("#titleName").val().trim();
                var beginDateStr = $("#startDate").val();
                var endDateStr = $("#endDate").val();
                var beginTime = $("#startTime").val();
                var endTime = $("#endTime").val();
                var maxAssoNum = $("#maxNumber").val();
                var data = {
                    title: title,
                    applyId: applyId,
                    beginDate: beginDateStr,
                    endDate: endDateStr,
                    beginTime: beginTime,
                    endTime: endTime,
                    maxAssoNum: maxAssoNum,
                    status: status,
                    id: newAssocaitionId
                };
                baseutil.post('/work/associationNew/wxassociationnew/assocaitionSave', data, function (res) {
                    if (status == "3") {
                        dialog.msg("保存成功");
                    } else {
                        dialog.msg("发布成功");
                    }
                    //刷新上一个界面的操作
                   parent.tabRefresh(menuId);
                    if (res.code == "0") {
                        parent.tabDelete('associationEdit');
                    } else {
                        if (status == "3") {
                            dialog.msg("保存失败");
                        } else {
                            dialog.msg("发布失败");
                        }
                    }
                });
            }
        }




        // 进行分页
        function toPage() {
            // 调用分页
            layui.use('laypage', function () {
                var laypage = layui.laypage;
                // 调用分页
                laypage.render({
                    elem: 'page',// *必选参数
                    count: total,// *必选参数
                    limits: [10, 25, 50, 100],
                    // 自定义首页、尾页、上一页、下一页文本
                    first: '首页',
                    last: '尾页',
                    prev: '<em><<</em>',
                    next: '<em>>></em>',
                    // 自定义主题
                    theme: "#FF5722",
                    // 自定义排版
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
                    // 渲染数据
                    jump: function (data, first) {
                        // data包含了当前分页的所有参数
                        curr = data.curr;
                        limit = data.limit;

                        // 首次不执行
                        if (!first) {
                            getInfo();
                        }
                    }
                });
            })
        }

        // 进行分页
        function toPageOther() {
            // 调用分页
            layui.use('laypage', function () {
                var laypage = layui.laypage;
                // 调用分页
                laypage.render({
                    elem: 'pageother',// *必选参数
                    count: total,// *必选参数
                    limits: [10, 25, 50, 100],
                    // 自定义首页、尾页、上一页、下一页文本
                    first: '首页',
                    last: '尾页',
                    prev: '<em><<</em>',
                    next: '<em>>></em>',
                    // 自定义主题
                    theme: "#FF5722",
                    // 自定义排版
                    layout: ['count', 'prev', 'page', 'next', 'limit', 'skip'],
                    // 渲染数据
                    jump: function (data, first) {
                        // data包含了当前分页的所有参数
                        curr = data.curr;
                        limit = data.limit;

                        // 首次不执行
                        if (!first) {
                            getassociationMessage(newAssocaitionId);
                        }
                    }
                });
            })
        }

        $("#exportExcel").on("click",function () {
            window.location.href = "/work/wxassociationnewstu/wxassociationnewstu/export";
        });
        $("#addassociation").on("click",function () {
            if(validNewBaseMsg()){
                dialog.popUpPage('添加社团','/work/associationNew/wxassociationnew/selectAssociation?selectIds='+associationSelectIds+'&applyId='+applyId,"700px","620px", function (layer, index) {
                    var body = layer.getChildFrame('body', index);
                    body.find("#save").bind("click", function () {
                        parent.layer.closeAll();
                        //把相应的值获取到
                       var mark =  body.find("#association").val();
                        addAssocationByDialg(mark);
                    });
                    body.find("#cancel").bind("click", function () {
                        parent.layer.closeAll();
                    });
                });
            }
        });

        $("#addassociationSet").on("click",function () {
            dialog.popUpPage('设置招新人数',"/work/associationNew/wxassociationnew/setAssociationPeople?associationNewId="+newAssocaitionId+'&applyId='+applyId,"700px","620px", function (layer, index) {
                var body = layer.getChildFrame('body', index);
                body.find("#save").bind("click", function () {
                    parent.layer.closeAll();
                    //把相应的值获取到
                    var mark =  body.find("#associationNumber").val();
                    addAssociationPeople(mark);
                });
                body.find("#cancel").bind("click", function () {
                    parent.layer.closeAll();
                });
            });
        });

        $("#public").on("click",function () {
            updateAssciation("0");
        });

        $("#comment").on("click",function () {
            updateAssciation("3");

        });

        $("#cancel").on("click",function () {
            parent.tabDelete('associationEdit');
        });

    });
</script>
</body>
</html>